package hx.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据库中的时间字段我们尽量使用long类型，而不是DateTime类型，效率会提高
 * 有了这个工具类，基本操作也都挺方便了
 *
 * @author HX<titanrain@163.com>
 */
public class DateUtil {

    public static long currentTime() {
        return new Date().getTime();
    }

    public static Date getDate(long unixTime) {
        return new Date(unixTime);
    }

    public static Date getDate(String unixTime) {
        return new Date(Long.parseLong(unixTime));
    }

    public static String getDateStr(long unixTime) {
        Date date = getDate(unixTime);
        return getDateStr(unixTime, null);
    }

    public static String getDateStr(long unixTime, String pattern) {
        Date date = getDate(unixTime);
        if (pattern == null || pattern.equals("")) {
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        } else {
            return new SimpleDateFormat(pattern).format(date);
        }
    }

    public static String getDateStr(String unixTime) {
        return getDateStr(Long.parseLong(unixTime), null);
    }

    public static String getDateStr(String unixTime, String pattern) {
        return getDateStr(Long.parseLong(unixTime), pattern);
    }

    public static String getDateTimeStr(long unixTime) {
        return getDateTimeStr(unixTime, null);
    }

    public static String getDateTimeStr(long unixTime, String pattern) {
        Date date = getDate(unixTime);
        if (pattern == null || pattern.equals("")) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        } else {
            return new SimpleDateFormat(pattern).format(date);
        }
    }

    public static String getDateTimeStr(String unixTime) {
        return getDateTimeStr(Long.parseLong(unixTime), null);
    }

    public static String getDateTimeStr(String unixTime, String pattern) {
        return getDateTimeStr(Long.parseLong(unixTime), pattern);
    }
}
